//
//  FixturesResultViewController.m
//  KG
//
//  Created by Eugene Braginets on 14.03.12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "AppDelegate.h"
#import "FixturesResultViewController.h"
#import <QuartzCore/QuartzCore.h>
#import "bannerView.h"
#import "GamesViewController.h"



@implementation FixturesResultViewController
@synthesize bannerView=_bannerView;
@synthesize webView=_webView;
@synthesize tableView=_tableView;
@synthesize bannerViewController=_bannerViewController;


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
 
    
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    
    bannerView *bannerVC=[[bannerView alloc] init];
    [self.view addSubview:bannerVC.view];
    [bannerVC animate];
    self.bannerViewController=bannerVC;
    

    
    [super viewDidLoad];
    
    

    
    // Do any additional setup after loading the view from its nib.
}

- (void)viewDidUnload
{
    [self setBannerView:nil];
    [self setWebView:nil];
    [self setTableView:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (void) viewWillAppear:(BOOL)animated {
    self.title=@"Fixtures and Results";
}
#pragma mark - TableView  delegate

// Customize the number of sections in the table view.

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 50;    
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}


// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 2;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    CGRect rectLabel=CGRectMake(0, 10, 320, 40);
    CGRect rectView=CGRectMake(0, 0, 320, 50);
    
    UIView* view=[[UIView alloc] initWithFrame:rectView];
    UILabel* label=[[UILabel alloc] initWithFrame:rectLabel];
    
    switch (section) {
        case 0:
            label.text=@"  FOOTBALL";
            break;
        case 1:
            label.text=@"  HURLING";
            break;
            
        default:
            break;
    }
  
    [label setBackgroundColor:[UIColor colorWithRed:.5 green:.6 blue:.1 alpha:0.7]];

    
//    [view setBackgroundColor:[UIColor clearColor]];
    [view setBackgroundColor:[UIColor clearColor]];
    [view setContentMode:UIViewContentModeBottom];
//    [label setTextColor:[UIColor colorWithRed:.1 green:.3 blue:.1 alpha:1]];
    [label setTextColor:[UIColor whiteColor]];
    
    [label setFont:[UIFont boldSystemFontOfSize:20]];
    [label setMinimumFontSize:10];
    [view addSubview:label];
    return view;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    CGRect rect=CGRectMake(0, 0, 320, 10);
    UIView* view=[[UIView alloc] initWithFrame:rect];
    [view setBackgroundColor:[UIColor clearColor]];
    return view;
}


//- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
//    return @"    HEADER";
//}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 50;    
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 10;    
}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    static NSString *MyIdentifier = @"MyIdentifier";
    MyIdentifier = @"tblCellView";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:MyIdentifier];
    }
    
//	cell.textLabel.text=@"test test";
    
    switch (indexPath.section) {
        case 0:
            switch (indexPath.row) {
                case 0:
                    cell.textLabel.text=@"Football Fixtures";
                    break;
                case 1:
                    cell.textLabel.text=@"Football Results";
                    break;
                default:
                    break;
            }

            break;
        case 1:
            switch (indexPath.row) {
                case 0:
                    cell.textLabel.text=@"Hurling Fixtures";
                    break;
                case 1:
                    cell.textLabel.text=@"Hurling Results";
                    break;
                default:
                    break;
            }
            
            break;    
        default:
            break;
    }
    //    cell.detailTextLabel.text=@"details";
 
//    CGRect frame=CGRectMake(0, 0, 320, 70);
//    CGRect frame1=CGRectMake(0, 0, 320, 60);
//    
    UIView *bgColorView = [[UIView alloc] init];
    [bgColorView setBackgroundColor:[UIColor colorWithRed:.25 green:.3 blue:.05 alpha:0.7]];
//    bgColorView.layer.cornerRadius = 10;
//    UIView* bgColorView1=[[UIView alloc] initWithFrame:frame1];
//    [bgColorView1 addSubview:bgColorView];
//    [bgColorView1 setContentMode:UIViewContentModeTop];

    cell.selectedBackgroundView=bgColorView;
    
    [cell setSelectionStyle:UITableViewCellSelectionStyleGray];
    [cell setAccessoryView:[self makeDetailDisclosureButton]];
    
    [cell.textLabel setHighlightedTextColor:[UIColor whiteColor]];
    [cell.detailTextLabel setHighlightedTextColor:[UIColor whiteColor]];
    [cell setBackgroundColor:[UIColor whiteColor]];
//    [cell.textLabel setFont:[UIFont fontWithName:@"arial" size:36]];
    
    //[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
    
    return cell;
}


- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    [cell setBackgroundColor:[UIColor clearColor]];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSLog (@"selected row %d", indexPath.row);
    self.title=@"Back";
    
    
    switch (indexPath.section) {
        case 0: {
            
            switch (indexPath.row) {
                case 0: {
                    
                    [myAppDelegate getData:[NSString stringWithFormat:@"%@compLevel=Senior",[kReqArrayFootballFixtures objectAtIndex:0]]
                          andType:[NSString stringWithFormat:@"%@%@%@%@",kGameItem,kFootFixtures,[kCompPanel objectAtIndex:0],[kCompLevels objectAtIndex:0]]];

                    
                    GamesViewController* viewController=[[GamesViewController alloc] initWithItem:[NSString stringWithFormat:@"%@%@%@%@",kGameItem,kFootFixtures,[kCompPanel objectAtIndex:0],[kCompLevels objectAtIndex:0]]];
                    viewController.title=kFootFixtures;
                    viewController.reqPanels=kReqArrayFootballFixtures;
                    [self.navigationController pushViewController:viewController animated:YES];

                }
                    break;
                case 1: {
                    
                    [myAppDelegate getData:[NSString stringWithFormat:@"%@compLevel=Senior",[kReqArrayFootballResults objectAtIndex:0]]
                          andType:[NSString stringWithFormat:@"%@%@%@%@",kGameItem,kFootResults,[kCompPanel objectAtIndex:0],[kCompLevels objectAtIndex:0]]];
                    
                    GamesViewController* viewController=[[GamesViewController alloc] initWithItem:[NSString stringWithFormat:@"%@%@%@%@",kGameItem,kFootResults,[kCompPanel objectAtIndex:0],[kCompLevels objectAtIndex:0]]];
                    viewController.title=kFootResults;
                    viewController.reqPanels=kReqArrayFootballResults;
                    [self.navigationController pushViewController:viewController animated:YES];
                    
                }
            }
        }
            break;
                    
        case 1: {    
            switch (indexPath.row) {
                   case 0: {
                        
                       [myAppDelegate getData:[NSString stringWithFormat:@"%@compLevel=Senior",[kReqArrayHurlingFixtures objectAtIndex:0]]
                             andType:[NSString stringWithFormat:@"%@%@%@%@",kGameItem,kHurlFixtures,[kCompPanel objectAtIndex:0],[kCompLevels objectAtIndex:0]]];
                       
                       GamesViewController* viewController=[[GamesViewController alloc] initWithItem:[NSString stringWithFormat:@"%@%@%@%@",kGameItem,kHurlFixtures,[kCompPanel objectAtIndex:0],[kCompLevels objectAtIndex:0]]];
                        viewController.title=kHurlFixtures;
                        viewController.reqPanels=kReqArrayHurlingFixtures;
                        [self.navigationController pushViewController:viewController animated:YES];
                        
                    }
                        break;
                    case 1: {
                       
                        [myAppDelegate getData:[NSString stringWithFormat:@"%@compLevel=Senior",[kReqArrayHurlingResults objectAtIndex:0]]
                              andType:[NSString stringWithFormat:@"%@%@%@%@",kGameItem,kHurlResults,[kCompPanel objectAtIndex:0],[kCompLevels objectAtIndex:0]]];
                        
                        GamesViewController* viewController=[[GamesViewController alloc] initWithItem:[NSString stringWithFormat:@"%@%@%@%@",kGameItem,kHurlResults,[kCompPanel objectAtIndex:0],[kCompLevels objectAtIndex:0]]];
                        viewController.title=kHurlResults;
                        viewController.reqPanels=kReqArrayHurlingResults;
                        [self.navigationController pushViewController:viewController animated:YES];
                        
                    }
                    break; 
            }
                    
        }
    }
}

- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath {
    NSLog (@"selected accessory from row %d", indexPath.row);
    self.title=@"Back";
GamesViewController* viewController=[[GamesViewController alloc] init];
    viewController.title=@"Football Results";
    [self.navigationController pushViewController:viewController animated:YES];
    
}

//helper for accessoryButtonTapped
- (void) accessoryButtonTapped: (UIControl *) button withEvent: (UIEvent *) event
{
    NSIndexPath * indexPath = [self.tableView indexPathForRowAtPoint: [[[event touchesForView: button] anyObject] locationInView: self.tableView]];
    if ( indexPath == nil )
        return;
    [self.tableView.delegate tableView: self.tableView accessoryButtonTappedForRowWithIndexPath: indexPath];
}


- (UIButton *) makeDetailDisclosureButton {
    UIButton * button =[UIButton buttonWithType:UIButtonTypeCustom];
    [button setImage:[UIImage imageNamed:@"rightarrow"] forState:UIControlStateNormal];
    [button setBackgroundColor:[UIColor clearColor]];
    [button setFrame:CGRectMake(0, 0, 25, 25)];
    
    [button setImage:[UIImage imageNamed:@"rightarrow_pressed"] forState:UIControlStateHighlighted];
    [button setImage:[UIImage imageNamed:@"rightarrow_pressed"] forState:UIControlStateSelected];
    
    
    [button addTarget: self
               action: @selector(accessoryButtonTapped:withEvent:)
     forControlEvents: UIControlEventTouchUpInside];
    
    return ( button );
}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end
